package com.lianniu.mgr.cms.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.lianniu.mgr.base.BaseResult;
import com.lianniu.mgr.cms.model.NavigationVo;
import com.lianniu.mgr.cms.service.INavigationService;
import com.lianniu.mgr.pagination.PaginationInfo;

@Controller
@RequestMapping("/rest/navigation")
public class NavigationRestController {

private final static Logger logger = Logger.getLogger(NavigationRestController.class);
	
	@Autowired
	private INavigationService navigationService;
	
	@RequestMapping(value="/NavigationList",method={RequestMethod.GET,RequestMethod.POST})
	public @ResponseBody BaseResult NavigationList(HttpServletRequest request, NavigationVo curVo, PaginationInfo paginationInfo)
	 {
	       logger.info("NavigationRestController  NavigationList begin.");
	       try {
	           
	           logger.info("NavigationList begin.");          
	           BaseResult baseResult = navigationService.selectByNavigationForPage(curVo, paginationInfo);
	           logger.info("NavigationList end.");
	           logger.info("NavigationRestController NavigationList end.");
	           return baseResult;
	       } catch (Exception e) {
	       logger.error("NavigationRestController NavigationList error:" + e.getMessage());
	       logger.error("NavigationRestController NavigationList end.");
	       return BaseResult.exception(e.getMessage());
	     }
	  }
	
	@RequestMapping(value = {"/NavigationAdd"}, method = RequestMethod.POST)
	public @ResponseBody BaseResult NavigationAdd(HttpServletRequest request,NavigationVo curVo) {
	      logger.info("NavigationRestController NavigationAdd begin.");      
	      logger.info("NavigationRestController NavigationAdd:"+ JSON.toJSONString(curVo));
	      try {
	          logger.info("NavigationAdd begin.");
	        
	          BaseResult baseResult = navigationService.insertNavigation(curVo);
	          logger.info("NavigationAdd baseResult:" + JSON.toJSONString(baseResult));
	          logger.info("NavigationAdd end.");
	          return baseResult;

	    } catch (Exception e) {
	      logger.error("NavigationAdd error:"+e.getMessage());
	      logger.info("NavigationRestController NavigationAdd end.");
	      return BaseResult.exception(e.getMessage());
	    }

	  }
	
	 
	@RequestMapping(value = {"/NavigationEdit"}, method = RequestMethod.POST)
	  public @ResponseBody BaseResult NavigationEdit(HttpServletRequest request,NavigationVo curVo) {
	      logger.info("NavigationRestController NavigationEdit begin.");      
	      logger.info("NavigationRestController NavigationEdit:"+ JSON.toJSONString(curVo));
	      try {
	          logger.info("NavigationEdit begin.");
	          BaseResult baseResult = navigationService.updateById(curVo);
	          logger.info("NavigationEdit baseResult:" + JSON.toJSONString(baseResult));
	          logger.info("NavigationEdit end.");
	          return baseResult;

	    } catch (Exception e) {
	      logger.error("NavigationRestController error:"+e.getMessage());
	      logger.info("NavigationRestController NavigationEdit end.");
	      return BaseResult.exception(e.getMessage());
	    }
	  }
	  
	
	 @RequestMapping(value="/selectNavigationById",method={RequestMethod.GET,RequestMethod.POST})
	  public @ResponseBody BaseResult selectNavigationById(HttpServletRequest request,NavigationVo curVo) {
	        logger.info("NavigationRestController selectNavigationById begin.");
	        try {
	                    
	          BaseResult baseResult = navigationService.selectById(curVo);
	          logger.info("NavigationRestController end.");
	          logger.info("NavigationRestController selectNavigationById end.");
	          return baseResult;
	        } catch (Exception e) {
	          logger.error("NavigationRestController selectNavigationById error:" + e.getMessage());
	          logger.error("NavigationRestController selectNavigationById end.");
	          return BaseResult.exception(e.getMessage());
	        }
	  } 
	 
	 @RequestMapping(value = {"/NavigationDel"}, method = RequestMethod.POST)
	  public @ResponseBody BaseResult NavigationDel(HttpServletRequest request) {
	      logger.info("NavigationRestController NavigationDel begin.");
	     try {          
	          logger.info("NavigationDel begin.");
	          BaseResult baseResult = navigationService.deleteNavigationBatch(request.getParameter("ids").split(","));		
	          logger.info("NavigationDel baseResult:" + JSON.toJSONString(baseResult));
	          logger.info("NavigationDel end.");
	          
	          logger.info("NavigationRestController NavigationDel end.");
	          return baseResult;

	    } catch (Exception e) {
	      logger.error("NavigationRestController NavigationDel error:" + e.getMessage());
	      logger.info("NavigationRestController NavigationDel end.");
	      return BaseResult.exception(e.getMessage());
	    }

	  }
	 
	 @RequestMapping(value = {"/checktsnName"}, method = RequestMethod.POST)
	  public @ResponseBody Boolean checktsnName(HttpServletRequest request) {
	      Boolean flag=true;
	      String tsnName=  request.getParameter("tsnName");
	      logger.info("NavigationRestController checktsnName begin.");
	     try {          
	          logger.info("checktsnName begin.");
	          if("".equalsIgnoreCase(tsnName)||tsnName==null)
	          {return flag;}
	          flag = navigationService.checkTsnName(tsnName);         
	          logger.info("checktsnName end.");
	          
	          logger.info("NavigationRestController checktsnName end.");
	          return flag;
	    } catch (Exception e) {
	      logger.error("NavigationRestController checktsnName error:" + e.getMessage());
	      logger.info("NavigationRestController checktsnName end.");
	      return flag;
	    }

	  }
	 
	 @RequestMapping(value = {"/checkTsnNameEdit"}, method = RequestMethod.POST)
	  public @ResponseBody Boolean checkTsnNameEdit(HttpServletRequest request) {
	      Boolean flag=true;
	      String tsnName=  request.getParameter("tsnName");
	      String id=request.getParameter("id");
	      logger.info("NavigationRestController checkTsnNameEdit begin.");
	     try {          
	          logger.info("checkTsnNameEdit begin.");
	          NavigationVo vo=new NavigationVo();	         
	          vo.setId(id);
	          
	          BaseResult baseResult =navigationService.selectById(vo);	          
	          NavigationVo curVo=(NavigationVo) baseResult.getData();
	          if(curVo.getId().equals(id)&&curVo.getTsnName().equals(tsnName))
	          {return flag;}
	          if("".equalsIgnoreCase(tsnName)||tsnName==null)
	          {return flag;}
	          flag = navigationService.checkTsnName(tsnName);         
	          logger.info("checkTsnNameEdit end.");
	          
	          logger.info("NavigationRestController checkTsnNameEdit end.");
	          return flag;
	    } catch (Exception e) {
	      logger.error("NavigationRestController checkTsnNameEdit error:" + e.getMessage());
	      logger.info("NavigationRestController checkTsnNameEdit end.");
	      return flag;
	    }

	  }
}
